package com.jinxiangyang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jinxiangyang.domain.Menu;
import com.jinxiangyang.domain.Subject;
import com.jinxiangyang.mapper.MenuMapper;
import com.jinxiangyang.service.MenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 订单明细 订单明细 服务实现类
 * </p>
 *
 * @author jinxiangyang
 * @since 2023-08-12
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Override
    public List<Menu> getTreeNode(Integer id){
        QueryWrapper<Menu> qw = new QueryWrapper<>();
        qw.eq("parent_id",id).orderByAsc("sort");
        List<Menu> menusList = baseMapper.selectList(qw);
        for (Menu menu : menusList) {
            Long menuId = menu.getId();
            boolean isChild = this.isChildren(menuId);
            menu.setHasChildren(isChild);
        }
        return menusList;
    }

    private boolean isChildren(Long id) {
        QueryWrapper<Menu> qw = new QueryWrapper<>();
        qw.eq("parent_id",id);
        Long count =baseMapper.selectCount(qw);
        return count>0;
    }
}
